//引入对应的模块
import Vue from 'vue'
import VueRouter from 'vue-router'
import {routes} from './routes'
//2注册路由模块
Vue.use(VueRouter)


//解决所有编程式导航错误===》在原型上修改push和replace方法,用于解决重复跳转时报错的问题
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function(location,okCallBack,errCallBack){
    if(okCallBack === undefined && errCallBack === undefined){
        //没传递这俩参数
        originPush.call(this,location).catch(()=>{})
    }else {
        originPush.call(this,location,okCallBack,errCallBack)
    }

}
VueRouter.prototype.replace = function(location,okCallBack,errCallBack){
    if(okCallBack === undefined && errCallBack === undefined){
        //没有传递这两个参数
        originReplace.call(this,location).catch(()=>{})
    }else {
        originReplace.call(this,location,okCallBack,errCallBack)
    }

}

//设置路由信息
const router = new VueRouter({
    mode:'history',
    routes,
    scrollBehavior(to,from,savePosition){
        if(savePosition){
            return savePosition
        }else{
            return {
                x:0,y:0
            }
        }
    }

})
export default router